package com.ff.xyh.service.post.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.post.PostDelete;
import com.ff.xyh.common.qo.post.PostDeleteQuery;
import com.ff.xyh.service.post.service.PostDeleteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 帖子删除表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@RestController
@RequestMapping("/post-service")
public class PostDeleteController {


    @Autowired
    private PostDeleteService postDeleteService;

    @GetMapping("get-post-delete-all")
    public ResultObject getAllPostDelete() {
        List<PostDelete> list = postDeleteService.list(null);
        if (list == null || list.isEmpty()){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("items", list);
            return ResultObject.ok().data(map);
        }
    }

    @PostMapping("get-post-delete-page")
    public ResultObject getPostDeletePage (
            @RequestParam(name = "current") long currentPage,
            @RequestParam(name = "size") long size) {

        Page<PostDelete> postDeletePage = new Page<>(currentPage,size);
        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        postDeleteService.page(postDeletePage, null);
        long total = postDeletePage.getTotal();//总记录数
        List<PostDelete> postDeleteList = postDeletePage.getRecords(); //数据list集合
        if (postDeleteList == null || postDeleteList.isEmpty()) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("total", total);
            map.put("items", postDeleteList);
            return ResultObject.ok().data(map);
        }
    }

    /**
     * 条件查询
     * @param postDeleteQuery
     * @return
     */
    @PostMapping("get-post-delete-condition")
    public ResultObject getPostDeleteCondition(
            @RequestBody PostDeleteQuery postDeleteQuery) {
        return postDeleteService.getPostDeleteCondition(postDeleteQuery);

    }

    @GetMapping("get-post-delete-postId-postType")
    public ResultObject getPostDeleteByPostIdAndPostType(
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") Integer postType) {
        PostDelete postDelete = postDeleteService.getOne(new QueryWrapper<PostDelete>()
                .eq("post_id", postId).eq("post_type", postType));
        return ResultObject.ok().putData(postDelete);
    }

    @GetMapping("delete-post-delete-postId-postType")
    public ResultObject deletePostDeleteByPostIdAndPostType(
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") Integer postType) {
        QueryWrapper<PostDelete> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postId).eq("post_type", postType);

        PostDelete postDelete = postDeleteService.getOne(wrapper);
        if (postDelete == null){
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        } else {
            if (postDeleteService.remove(wrapper)) {
                return ResultObject.ok();
            } else {
                return ResultObject.ok().message(ResultMessage.FAILED_TO_DELETE);
            }
        }
    }

    @PostMapping("insert-post-delete")
    public ResultObject insertPostDelete (
            @RequestBody PostDelete postDelete) {
        QueryWrapper<PostDelete> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postDelete.getPostId())
                .eq("post_type", postDelete.getPostType());
        if (postDeleteService.getOne(wrapper) != null){
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_NOT_EXISTS);
        } else {
            if (postDeleteService.save(postDelete)) {
                return ResultObject.ok().putData(postDelete);
            } else {
                return ResultObject.ok().message(ResultMessage.FAILED_TO_INSERT);
            }
        }
    }

    @PostMapping("update-post-delete")
    public ResultObject updatePostDelete(
            @RequestBody PostDelete postDelete) {
        QueryWrapper<PostDelete> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postDelete.getPostId())
                .eq("post_type", postDelete.getPostType());
        if (postDeleteService.getOne(wrapper) == null){
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        } else {
            if (postDeleteService.update(postDelete,wrapper)) {
                return ResultObject.ok().putData(postDelete);
            } else {
                return ResultObject.ok().message(ResultMessage.FAILED_TO_UPDATE);
            }
        }
    }
}

